﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ivory;
using Schism.Cameras;
using Schism.Mapping;
using Schism.IO;
using Schism.Core;

namespace Schism
{
    public abstract class LevelBase : GameScreen
    {
        //Fields
        private string _GameDataFile;
        public int WorldWidth { get { return GameCellMap.MapWidth; } }
        public int WorldPixelWidth { get { return WorldWidth * Globals.GLOBAL_TILE_WIDTH; } }

        public int WorldHeight { get { return GameCellMap.MapHeight; } }
        public int WorldPixelHeight { get { return WorldHeight * Globals.GLOBAL_TILE_HEIGHT; } }

        //Game Objects    
        public CellMap GameCellMap;
        public LightingEngine LightingEngine;

        //Construct
        public LevelBase()
            : base(ObjectHolder.GameObject)
        {

        }
        
        //Methods
        public void SetGameDataFile(string datafile)
        {
            _GameDataFile = datafile;
        }
        public void LoadFrom(string dataFile)
        {
            WorldIO io = new WorldIO();
            io.Load(dataFile);
            io.Assign(this);
        }
        public void SaveAs(string dataFile)
        {
            WorldIO io = new WorldIO();
            io.ExtractFrom(this);
            io.Save(dataFile);
        }
        
        protected abstract void Render(SpriteBatch batch, GameTime time);
        protected abstract void Solve(InputControl input, GameTime time);
        protected abstract bool OnLoadingScreen();

        public override void RenderGame(SpriteBatch batch, GameTime time)
        {                        
            //Render Level Specific stuff e.g. GUI
            Render(batch, time);

        }
        public override void UpdateGame(GameTime time, InputControl input)
        {
            ObjectHolder.GameCamera.Solve(input, time);

            this.Solve(input, time);
        }

        public override void RenderUI(SpriteBatch batch, GameTime time) { }
        public override void UpdateUI(GameTime time, InputControl input) { }

        public override bool PreloadAssets()
        {
            this.LoadingScreen = new LoadScreen(Game, FontBank.GetLoc("Arial16"), Color.Black, Color.White);

            return true;
        }
        public override bool LoadData()
        {
            OnLoadingScreen();
            return true;
        }
    }
}
